package com.maixy.nettyserverdemo;

import io.netty.channel.EventLoop;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.util.concurrent.DefaultPromise;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ExecutionException;

/**
 * @ClassName TestNettyPromise
 * @Description TODO
 * @Date 2021/4/23 16:47
 * @Author maixy
 */
@Slf4j
public class TestNettyPromise {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //获得eventLoop对象
        EventLoop eventLoop = new NioEventLoopGroup().next();

        //可以主动创建promise，结果容器
        DefaultPromise<Integer> promise = new DefaultPromise<>(eventLoop);
        new Thread(() -> {
            //3.任意执行一个线程计算，计算完毕后向promise填充结果
            try {
                log.debug("计算结果");
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            promise.setSuccess(100);
        }).start();

        log.debug("等待结果");
        log.debug("结果是：{}",promise.get());

    }
}
